home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / educate / wordy442.zip / RF.C < prev    next >
C/C++ Source or Header  |  1996-06-20  |  9KB  |  320 lines

  1. /**************************************************************************/
  2. /*                                ReFormat Utility                        */
  3. /*                                                                        */
  4. /*                                                                        */
  5. /*                                     M\Cooper                           */
  6. /*                                    PO Box 237                          */
  7. /*                            St. David, AZ 85630-0237                    */
  8. /*                        -------------------------------                 */
  9. /*                        Email:  thegrendel@theriver.com                 */
  10. /*                                                                        */
  11. /*                  $2.00 to register the entire WORDY package            */
  12. /*                                                                        */
  13. /*      Reformats the files created by the pattern matching utilities     */
  14. /*          so that they print in an "optimum" number of columns.         */
  15. /*                                                                        */
  16. /**************************************************************************/
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <conio.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24.  
  25. #define FILE_OPENING_ERROR 3
  26. #define FILENAME_MAXLEN 8
  27. #define CR "\n"
  28. #define MAXLEN 82
  29. #define LINE_LEN 80
  30. #define NOARGS 1
  31. #define FULLARGS 3
  32. #define INCREMENT 1
  33. #define SPACE ' '
  34. #define MAXWORDLEN 24
  35. #define WORDSPACING 3
  36. //#define COLUMNS 5 (see below)
  37. #define BUFFERSIZE 4096
  38.  
  39. typedef enum { LESS_THAN, EQUAL, GREATER_THAN, ANOMALY } A_flag;
  40. typedef enum { OFF, ON } Mod_flag;
  41.  
  42.  
  43. void reformat( char *fname, int maxwordlength, int thr_wlen, A_flag action );
  44. A_flag parse_action( char *second_arg );
  45. int parse_threshhold ( char *second_arg );
  46. int get_max_word_length( char *fname );
  47. void center( char *strg ); 
  48.  
  49.    char ad[] =
  50.    "REFORMAT utility by M\\Cooper, PO Box 237, St. David, AZ 85630-0237";
  51.  
  52.  
  53.  
  54.  
  55. typedef enum { FALSE, TRUE } Boolean;
  56.  
  57. void main( int argc, char **argv )
  58. {
  59.  
  60.    char filename[ MAXLEN ];
  61.    int mwl,
  62.        threshhold = MAXWORDLEN;
  63.    A_flag action_flag;
  64.  
  65.      if( argc == NOARGS )
  66.         {
  67.         clrscr();
  68.         puts("Enter a FILENAME to reformat... ");
  69.         gets( filename );
  70.         }
  71.      else
  72.         strcpy( filename, *( argv + 1 ) );
  73.  
  74.       if( argc == FULLARGS )
  75.           {
  76.           action_flag = parse_action( *( argv + 2 ) );
  77.           threshhold =  parse_threshhold( *( argv + 2 ) );
  78.           }
  79.       else
  80.          {
  81.          action_flag = ANOMALY;
  82.          threshhold = MAXWORDLEN;
  83.          }
  84.  
  85.      mwl = get_max_word_length( filename );
  86.      reformat( filename, mwl, threshhold, action_flag );
  87. }
  88.  
  89.  
  90.  
  91.  
  92. /*************************************************************/
  93. void reformat( char *filename, int maxwl, int threshlen, A_flag action )
  94. {
  95.  
  96.     char    word [ MAXLEN ],
  97.       desc_messg [ MAXLEN ],
  98.       count_messg [ MAXLEN ],
  99.         tempstr [ MAXLEN + 3 ],
  100.         targetfile [ MAXLEN ],
  101.         tempfilename [ MAXWORDLEN ] = "tmp$$$.$$$",
  102.         bar [ LINE_LEN + 1 ],
  103.         double_bar [ LINE_LEN + 1 ];
  104.  
  105.     FILE *fptr,
  106.         *tfile;
  107.     long wcount = 0L;
  108.      int columns,
  109.          interval,
  110.          t_up = MAXWORDLEN,
  111.          t_down = 0;
  112.  
  113.  
  114.        memset( bar, '-', LINE_LEN );
  115.        *( bar + LINE_LEN ) = NULL;
  116.        memset( double_bar, '=', LINE_LEN );
  117.        *( double_bar + LINE_LEN ) = NULL;
  118.  
  119.        /*************opening credits*************/
  120.        clrscr();
  121.        printf( double_bar );
  122.        strcpy( tempstr, ad );
  123.        center ( tempstr );
  124.        printf( tempstr );
  125.        printf( CR );
  126.        printf( double_bar );
  127.        printf( CR );
  128.        /****************************************/
  129.  
  130.  
  131.        /*   Create name of temp file to store derived words in  */
  132.        /*********************************************************/
  133.        strcpy( targetfile, tempfilename );
  134.        /*********************************************************/
  135.  
  136.        if( !( fptr = fopen( filename, "rt" ) ) )
  137.          {
  138.          printf( "\7\7\7Cannot open file to reformat!" );
  139.          exit( FILE_OPENING_ERROR );
  140.          }
  141.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE ) )
  142.          exit( FILE_OPENING_ERROR + 1 );
  143.  
  144.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  145.          {
  146.          printf( "\7\7\7Cannot open temp working file!" );
  147.          exit ( FILE_OPENING_ERROR + 2 );
  148.          }
  149.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  150.          exit( FILE_OPENING_ERROR + 3 );
  151.  
  152.        /**************'Wait' Message************/
  153.        printf( CR CR );
  154.        printf( "WORKING...\n\n" );
  155.        printf( "Reformatting file %s.\n", filename );
  156.        printf( "It may take a few seconds, depending on file length.\n\n" );
  157.        /*****************************************/
  158.  
  159.  
  160.          /*********************Check for word limits*********************/     
  161.  
  162.       threshlen += 1; //Compensate for CR at end of each word.
  163.  
  164.       switch ( action )
  165.              {
  166.              case EQUAL:
  167.                   t_up = threshlen + 1;
  168.                   t_down = threshlen - 1;
  169.                   sprintf( desc_messg, "List restricted to words exactly %d letters long.", threshlen - 1 );
  170.                   break;
  171.              case LESS_THAN:
  172.                   t_up = threshlen;
  173.                   t_down = 0;
  174.                   sprintf( desc_messg, "List restricted to words less than %d letters long.", threshlen - 1 );
  175.                   break;
  176.              case GREATER_THAN:
  177.                   t_up = MAXWORDLEN;
  178.                   t_down = threshlen;
  179.                   sprintf( desc_messg, "List restricted to words more that %d letters long.", threshlen - 1 );     
  180.                   break;
  181.              case ANOMALY:        //accept all...
  182.                   t_up = MAXWORDLEN;
  183.                   t_down = 0;
  184.              default: 
  185.                   break;
  186.  
  187.              }
  188.       maxwl = maxwl > t_up ? t_up : maxwl;
  189.       //Readjust column width, as necessary.
  190.  
  191.        columns = LINE_LEN / ( maxwl + WORDSPACING );
  192.       interval = maxwl + WORDSPACING;
  193.  
  194.          /*********************Main Loop*************/     
  195.  
  196.       printf( "\nThe file will be reformatted in %d columns.", columns );
  197.  
  198.           while( fgets( word, MAXLEN, fptr ) != NULL )
  199.  
  200.                               /*****888copy action888***/
  201.     if( strlen( word ) > t_down &&
  202.        strlen( word ) < t_up &&
  203.        !isspace( *word )  )
  204.  
  205.  
  206.                {
  207.       wcount++;
  208.  
  209.                *( word + strlen( word ) -1 ) = NULL;  //Gets rid of CR
  210.  
  211.                fprintf( tfile, "%-*s", interval, word );
  212.  
  213.                if( !( wcount % columns ) )
  214.                   fprintf( tfile, CR );
  215.                }
  216.             else
  217.     if( strlen( word)  >= MAXWORDLEN  && !iscntrl( *word ) )
  218.                fprintf( tfile, "\n%s", word );
  219.  
  220.      /**************999*************/
  221.  
  222.  
  223.       sprintf( count_messg, "From the original list, %ld word(s) have been selected.", wcount );
  224.       center( count_messg );
  225.       fprintf( tfile, "\n\n%s", count_messg );
  226.       center ( desc_messg );
  227.       fprintf( tfile, "\n\n%s", desc_messg );
  228.  
  229.           /*******************************************/
  230.  
  231.           fcloseall();
  232.  
  233.           remove( filename );
  234.           rename( targetfile, filename );
  235.  
  236.           sprintf( tempstr,
  237.                  "The file %s has been reformatted.", filename );
  238.           center( tempstr );
  239.           printf( CR CR );
  240.           printf( tempstr );
  241.  
  242. }
  243.  
  244. int get_max_word_length( char *filename )
  245. {
  246.    int wlen,
  247.       maxwlen = 0; // Longest word in file
  248.    char word [ MAXLEN ];
  249.    FILE *fp;
  250.  
  251.       if( !( fp = fopen( filename, "rt" ) ) )
  252.             {
  253.             printf( "\7\7\7Cannot open file to reformat!" );
  254.             exit( FILE_OPENING_ERROR );
  255.             }
  256.  
  257.       clrscr();
  258.       printf( "Getting formatting information from file... \n" );
  259.       printf( "Please wait." );
  260.  
  261.       while( fgets( word, MAXLEN, fp ) != NULL )
  262.         {
  263.         wlen = strlen( word );
  264.         if( wlen > maxwlen && wlen < MAXWORDLEN )
  265.            maxwlen = wlen;  //Bump up to new value.
  266.         }
  267.  
  268.      return( maxwlen );
  269. }
  270.  
  271. void center( char *str )
  272. {
  273.    int padding;
  274.    char st [ LINE_LEN + INCREMENT ];
  275.  
  276.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  277.      memset( st, SPACE, padding );
  278.      *( st + padding ) = N